#include "common_main.h"
#include "util/log.h"
#include "game.h"
#include "lobby.h"
#include "main.h"
#include "menu.h"
#include "network.h"

void common_main_update_state ()
{
	switch (main_state) {

	case STATE_MENU:
		menu_update ();
		break;

	case STATE_LOBBY:
		lobby_update ();
		network_update ();
		break;

	case STATE_GAME:
		game_update ();
		network_update ();
		break;

	case STATE_INITIAL:
	case STATE_QUIT:
	default:
		assert(!"update() function is invalid for current state");

	}
}

void common_main_change_state (ApplicationState new_state)
{
	log_debug ("Change from state %d to state %d.", main_state, new_state);
	
	switch (main_state) {
	
	case STATE_INITIAL:
		break;
		
	case STATE_MENU:
		log_info ("Exit state MENU");
		menu_cleanup ();
		break;
		
	case STATE_LOBBY:
		log_info ("Exit state LOBBY");
		lobby_cleanup ();
		break;
		
	case STATE_GAME:
		log_info ("Exit state GAME");
		game_cleanup();
		break;
		
	case STATE_QUIT:
		assert(!"can never change back from quit state");
		
	default:
		assert(!"impossible main_state to change from");
	
	}
	
	main_state = new_state;
	
	switch (new_state) {
	
	case STATE_INITIAL:
		assert(!"can only change from (never to) initial state");
		
	case STATE_MENU:
		log_info ("Enter state MENU");
		menu_init ();
		break;
	
	case STATE_LOBBY:
		log_info ("Enter state LOBBY");
		lobby_init ();
		break;
		
	case STATE_GAME:
		log_info ("Enter state GAME");
		game_init();
		break;
	
	case STATE_QUIT:
		log_info ("Enter state QUIT, shutting down application...");
		break;
		
	default:
		assert(!"impossible new_state to change to");
	
	}
}
